<?php 
require_once ("../Login/config.php");
include_once '../Login/databaseconnection.php';
if ($_FILES["file"]["error"] > 0)
  {
 $_SESSION['error']="Error: " . $_FILES["file"]["error"] . "<br />";
  }
if (file_exists("Upload/" . $_FILES["file"]["name"]))
      {
     $_SESSION['errorexi']=$_FILES["file"]["name"] . " already exists.". "<br />";
      }
    else
      {
	  move_uploaded_file($_FILES["file"]["tmp_name"],
      "Upload/" . $_FILES["file"]["name"]);
	  $header = null; 
    $data = array(); 
    $csvData=str_getcsv(file_get_contents('Upload/'.$_FILES["file"]["name"]), "\n"); 
    $delimiter = ",";
    foreach($csvData as $csvLine){ 

if(is_null($header))
{ 
$header = explode($delimiter, $csvLine);
} 
	  $m = count($header);
	  $m = count($header);
		 $sqlcols=mysql_query("SHOW COLUMNS FROM exam");
		 $nr=mysql_num_rows($sqlcols);
		 }
		 if($m!=$nr-1)
		{
		$_SESSION['errorinuploading']="Please check the inputs in the CSV file!<br/>";
		}
		if($m==$nr-1)
		{    
    $header = null; 
    $data = array(); 
    $csvData=str_getcsv(file_get_contents('Upload/'.$_FILES["file"]["name"]), "\n"); 
	$file_name='Upload/'.$_FILES["file"]["name"];
    $delimiter = ",";
    foreach($csvData as $csvLine){ 

if(is_null($header))
{ $header = explode($delimiter, $csvLine);
				
} 
        else{
		date_default_timezone_set("Asia/Colombo");
    $t_d=date("Y/m/d");
		$m = count($header);
		 $sqlcols=mysql_query("SHOW COLUMNS FROM exam");
		 $nr=mysql_num_rows($sqlcols);
		
		if($m==$nr-1)
		{
           
            $items = explode($delimiter, $csvLine); 
			if($items[0]==""||$items[1]==""||$items[2]=="")
			{
			$_SESSION['errordata']="Not enough inputs Please recheck the inputs and try again!<br/>";
			}
			else
			{
			$checking=mysql_query("SELECT * FROM exam WHERE School_Id='$items[0]' AND Exam_Name='$items[1]' AND Year='$items[2]'");
			if($checking)
			{
			if(mysql_num_rows($checking)!=0)
			{
				$_SESSION['errorexi']=$_SESSION['errorexi'].'<img src="../images/alert.jpg" width="12" height="12"/>'.$items[0].",".$items[1].",".$items[2]." Already exists!<br/>";
			}
			else
			{
             $sql="INSERT INTO exam(School_Id,Exam_Name,Subject,Year,No_Of_Sat_Students,No_Of_Passed_Students,Last_Updated_Date) VALUES(";
            for($n = 0, $m = count($header); $n < $m; $n++)
			{ 
                $sql.= "'".$items[$n]."',";
			} 
				 if($n == $nr-1) 
				 {
					$sql.= "'".$t_d."'";
					}
			$sql.= ")";
            $inse=mysql_query($sql);
			if(mysql_affected_rows==0)
			 {			 
             unlink("$file_name"); 
			 }
			else if($inse&&isset($_SESSION['errorexi']))
			{
			$_SESSION['insertsucc']="Others are successfully inserted!";
			}
			else if($inse&&!isset($_SESSION['errorexi']))
			{
			if($m>1)
			{
			$_SESSION['insertsucc']="All are successfully inserted!";
			}
			if($m==1)
			{
			$_SESSION['insertsucc']="Exam's details are successfully inserted!";
			}
			}
			}
			}
			}
          }
        } 
    } 
	}
	}
 mysql_close($conn);
?> 